﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainWindowViewModel.cs" company="Akzente.IT">
//   (c) 2012 Akzente.IT
// </copyright>
// <summary>
//   View model for the MainWindow class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AkzenteIT.DatabaseDocumentationTool.ViewModels
{
    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Text;

    using AkzenteIT.DatabaseDocumentationTool.DocScriptGenerator;
    using AkzenteIT.DatabaseDocumentationTool.ViewModels.Base;

    /// <summary>
    /// View model for the MainWindow class.
    /// </summary>
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        #region Fields

        /// <summary>
        /// The generator.
        /// </summary>
        private readonly ScriptGenerator generator = new ScriptGenerator();

        /// <summary>
        /// The selected node.
        /// </summary>
        private NodeViewModel selectedNode;

        /// <summary>
        /// The servers.
        /// </summary>
        private ObservableCollection<ServerViewModel> servers = new ObservableCollection<ServerViewModel>();

        #endregion

        #region Public Events

        /// <summary>
        /// The property changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the script.
        /// </summary>
        public string Script
        {
            get
            {
                if (this.selectedNode == null)
                {
                    return string.Empty;
                }

                StringBuilder builder = new StringBuilder();
                this.generator.GenerateScript(this.selectedNode, builder);

                return builder.ToString();
            }
        }

        /// <summary>
        /// Gets or sets the scripting depth.
        /// </summary>
        public int ScriptingDepth
        {
            get
            {
                return this.generator.RecursionDepth;
            }

            set
            {
                this.generator.RecursionDepth = value;
                this.NotifyPropertyChanged("ScriptingDepth");
                this.NotifyPropertyChanged("Script");
            }
        }

        /// <summary>
        /// Gets or sets the selected node.
        /// </summary>
        public NodeViewModel SelectedNode
        {
            get
            {
                return this.selectedNode;
            }

            set
            {
                if (this.selectedNode != null)
                {
                    this.selectedNode.NodeUpdated -= this.SelectedNodeUpdated;
                }

                this.selectedNode = value;
                this.NotifyPropertyChanged("SelectedNode");
                this.NotifyPropertyChanged("Script");

                if (this.selectedNode != null)
                {
                    this.selectedNode.NodeUpdated += this.SelectedNodeUpdated;
                }
            }
        }

        /// <summary>
        /// Gets or sets the servers.
        /// </summary>
        public ObservableCollection<ServerViewModel> Servers
        {
            get
            {
                return this.servers;
            }

            set
            {
                this.servers = value;
                this.NotifyPropertyChanged("Servers");
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The notify property changed.
        /// </summary>
        /// <param name="propName">
        /// The prop name.
        /// </param>
        public void NotifyPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The selected node_ node updated.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void SelectedNodeUpdated(object sender, EventArgs e)
        {
            this.NotifyPropertyChanged("Script");
        }

        #endregion
    }
}